package com.aaa.controller;

import com.aaa.entity.Dept;
import com.aaa.service.DeptService;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 部门表(Dept)表控制层
 *
 * @author makejava
 * @since 2022-11-17 14:38:40
 */
@RestController
@RequestMapping("dept")
@Api(tags = "部门增删查改")
public class DeptController {
    /**
     * 服务对象
     */
    @Resource
    private DeptService deptService;

    /**
     * 查询页面
     * 分页查询
     *
     * @param currentPage 当前页面
     * @param pageSize    页面大小
     * @return 查询结果
     */
    @ApiOperation("分页查询部门表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码,默认1", example = "1",
                    paramType = "query", dataType = "int", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数,默认5", example = "10",
                    paramType = "query", dataType = "int", required = false)
    })
    @GetMapping
    public Result<Page<Dept>> queryByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                          @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<Dept> page = deptService.page(new Page<>(currentPage, pageSize), null);
        return Result.success(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "int")
    )
    @GetMapping("{id}")
    public Result<Dept> queryById(@PathVariable("id") Integer id) {
        return Result.success(this.deptService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dept 实体
     * @return 新增结果
     */
    @ApiOperation("添加部门接口")
    @PostMapping
    public Result<Boolean> add(@RequestBody Dept dept) {
        return Result.success(this.deptService.save(dept));
    }

    /**
     * 编辑数据
     *
     * @param dept 实体
     * @return 编辑结果
     */
    @ApiOperation(value = "更新部门接口", notes = "测试requestBody")
    @PutMapping
    public Result<Boolean> edit(@RequestBody Dept dept) {
        return Result.success(deptService.updateById(dept));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键部门ID", example = "55",
                    paramType = "path", dataType = "int", required = true)
    })
    @DeleteMapping
    public Result<Boolean> deleteById(Integer id) {
        return Result.success(deptService.removeById(id));
    }
}

